package com.zhouyu.aop;

import com.zhouyu.service.UserService;
import org.springframework.cglib.proxy.*;

import java.lang.reflect.Method;

/**
 * @author mimao funfind@yeah.net
 * @date ${{
 * cjlb
 *
 */
public class Test {

	public static void main(String[] args) {

		UserService target = new UserService();

		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(UserService.class);

		// 定义额外逻辑，也就是代理逻辑
		enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
			@Override
			// o 代理对象
			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
				System.out.println("before...");
				Object result = methodProxy.invoke(target, objects);
//				Object result1 = method.invoke(target, objects);
				System.out.println("after...");
				return result; } }, NoOp.INSTANCE}
				);

		enhancer.setCallbackFilter(new CallbackFilter() {
			@Override
			public int accept(Method method) {
				if (method.getName().equals("test")){
					return 0;
				}else{
					return 1;
				}

			}
		});

		// 动态代理所创建出来的UserService对象
		UserService userService = (UserService) enhancer.create();
		// 执行这个userService的test方法时，就会额外会执行一些其他逻辑
		userService.test();
//		userService.c();
	}
}
